import re
import os

def clean_text(text):
    # 删除中文标点、阿拉伯数字和中文数字
    text = re.sub(r'[一二三四五六七八九十]、', '', text)  # 删除中文数字
    text = re.sub(r'[、。.]', '', text)  # 删除中文标点
    text = re.sub(r'\d+', '', text)  # 删除数字
    # 去除多余的空格
    text = re.sub(r' +', '', text)

    return text

def clean_line(line):
    # 去除多余的空格
    line = re.sub(r' +', '', line).strip()
    return line

def trans_special_chars(header):
    # 转义正则表达式的特殊字符，但保留空格
    special_chars = r"\.^$*+?{}[]|()"
    for char in special_chars:
        header = header.replace(char, "\\" + char)
    return header

def create_headers_pattern(headers):
    header_pattern = []
    for header in headers:
        if header:
            pattern = r"#\s*" + trans_special_chars(header) + r"\s*"
            print(pattern)
            header_pattern.append(pattern)

    # 合并所有标题模式
    combined_pattern = '|'.join(header_pattern)

    return combined_pattern

def split_text_by_headers(path, patterns):
    # 使用正则表达式找到所有标题及其位置
    cnt =0
    headers = []
    with open(path, "r", encoding="utf-8") as doc:
        for line in doc:
            cnt += 1
            # cleaned_line = clean_line(line)
            if re.match(patterns, line):
                headers.append(cnt)

    with open(path, "r", encoding="utf-8") as doc:
        input_text = doc.read()

    # 将文本按行分割成列表
    lines = input_text.split('\n')

    # 按headers中的行号分割文本
    part = []
    for i in range(len(headers)):
        if i == len(headers) - 1:
            start = headers[i] - 1
            chunk = ''.join(lines[start:])
            chunk = clean_line(chunk)
            part.append(chunk+'\n')
        else:
            start = headers[i] - 1
            end = headers[i + 1] - 1
            chunk = ''.join(lines[start:end])
            chunk = clean_line(chunk)
            part.append(chunk+'\n')

    return part

if __name__ == '__main__':
    # 指定目录文件路径
    input_contents_path = "tmp_txt/02_clean/contents/03_银河麒麟桌面操作系统V10 SP1 2503产品安装手册.txt"
    with open(input_contents_path, "r", encoding="utf-8") as doc:
        input_contents = doc.read()

    output_header_path = "tmp_txt/03_split/header/03_header_银河麒麟桌面操作系统V10 SP1 2503产品安装手册.txt"

    # 清理文本
    cleaned_header_text = clean_text(input_contents)

    # 保存分割文本（header）
    with open(output_header_path, "w", encoding="utf-8") as output_file:
            output_file.write(cleaned_header_text)

    print(f"分割后的标题已成功保存到 {output_header_path}")

    print("\n-------------------------------------------------------\n")

    # 指定目录文件路径
    input_path = "tmp_txt/02_clean/03_银河麒麟桌面操作系统V10 SP1 2503产品安装手册.txt"

    output_path = "tmp_txt/03_split/03_银河麒麟桌面操作系统V10 SP1 2503产品安装手册.txt"

    # 按行分割文本
    headers = input_contents.split("\n")

    patterns = create_headers_pattern(headers)

    sections = split_text_by_headers(input_path, patterns)

    # 保存分割文本（整体）
    with open(output_path, "w", encoding="utf-8") as output_file:
        for content in sections:

            output_file.write(content)

    print(f"分割后的文本已成功保存到 {output_path}")